package org.example.myleet.p744;

public class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int l = 0, r = letters.length - 1, m = 0;
        while (l < r) {
            m = (r + l ) >>> 1;
            if (letters[m] > target) {
                r = m;
            } else if (letters[m] < target) {
                l = m + 1;
            } else {
                l = m;
                break;
            }
        }
        if (letters[l] > target) {
            return letters[l];
        }
        do {
            ++l;
        } while (l < letters.length && letters[l] <= target);
        if (l >= letters.length) {
            return letters[0];
        }
        return letters[l];
    }
}
